<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/8/28
 * Time: 15:20
 */

namespace app\api\controller;


use think\Db;

class My extends Base
{
    private $user_id; // 用户ID
    private $page; // 页码
    
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $this->user_id = input('user_id');
        $this->page = input('page') ? input('page') : 1;
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($this->user_id,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->del]);
            exit;
        }
    }
    
    /**
     * 选择年级
     * @return \think\response\Json
     * grade_id   年级ID
     */
    public function chooseGrade(){
        $grade_id = input('grade_id');
        $res = Db::table('up_u_user')
            ->where('user_id', $this->user_id)
            ->update(['user_grade' => $grade_id]);
        if($res){
            return json(['status' => 200, 'msg' => '选择成功']);
        }else{
            return json(['status' => 0, 'msg' => '选择失败']);
        }
    }
    
    /**
     * 学生端用户协议
     * @return \think\response\Json
     */
    public function registerAgreement(){
        $agreement = Db::table('up_about')->where('about_id', 8)->value('about_student_agreement');
        if($agreement){
            return json(['status' => 200, 'msg' => $agreement]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 获取省市区的三维数组
     * @return \think\response\Json
     */
    public function getArea()
    {
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        
        $provinceLists = Db::name('area')->where('level', 1)->select();//省
        $cityLists = Db::name('area')->where('level', 2)->select();//市
        $districtLists = Db::name('area')->where('level', 3)->select();//区
        foreach($cityLists as $k => $city) {
            foreach($districtLists as $kk => $district) {
                if($city['district_id'] == $district['pid']) {
                    $cityLists[$k]['qu'][] = $district;
                }
            }
        }
        foreach($provinceLists as $key => $province) {
            foreach($cityLists as $ke => $city) {
                if($province['district_id'] == $city['pid']) {
                    $provinceLists[$key]['city'][] = $city;
                }
            }
        }
        return json(['status' => 200, 'msg' => $provinceLists]);
    }
    
    /**
     * 我的钱包
     * @return \think\response\Json
     */
    public function myMoney(){
        $balance = Db::table('up_u_user')->where('user_id', $this->user_id)->value('user_balance');
        return json(['status' => 200, 'msg' => $balance]);
    }
    
    /**
     * 我的关注
     * @return \think\response\Json
     * page    当前页码
     */
    public function myAtt(){
        $list = Db::table('up_follow_user')
            ->join('up_u_user', 'user_id = follow_quilt_user_id', 'left')
            ->join('up_grade', 'user_grade = grade_id', 'left')
            ->field('user_username, user_image, user_gender, grade_name, user_status, user_online, 
            user_intro, user_id')
            ->where('follow_user_id', $this->user_id)
            ->order('user_is_top asc, user_sort desc')
            ->page($this->page, 10)
            ->select();
        if($list){
            foreach($list as $k => $v){
                // 星级
                $star_num = $this->teacherStar($v['user_id']);
                $list[$k]['star_num'] = $star_num;
            }
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 推送开关
     * @return \think\response\Json
     */
    public function showSend(){
        $is_send = Db::table('up_u_user')->where('user_id', $this->user_id)->value('user_is_send');
        return json(['status' => 200, 'msg' => $is_send]);
    }
    
    /**
     * 打开/关闭消息推送开关
     * @return \think\response\Json
     */
    public function doSend(){
        $is_send = Db::table('up_u_user')->where('user_id', $this->user_id)->value('user_is_send');
        if($is_send == 1){
            $user_is_send = 2;
        }else{
            $user_is_send = 1;
        }
        $res = Db::table('up_u_user')->where('user_id', $this->user_id)->setField('user_is_send', $user_is_send);
        if($res){
            return json(['status' => 200, 'msg' => '操作成功']);
        }else{
            return json(['status' => 0, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 更换手机号
     * @return \think\response\Json
     * phone  新的手机号
     * code   验证码
     * type   1 更换手机号第一步    2 更换手机号第二步
     */
    public function changePhone(){
        $phone = trim(input('phone'), ' ');
        $code = trim(input('code'), ' ');
        $type = input('type');
        if($type == 2){
            // 必填项
            if($phone == '' || $code == ''){
                return json(['status' => 0, 'msg' => '有未填项']);
            }
            
            // 手机号格式
            if(!preg_match("/^1[3456789]\d{9}$/", $phone)) {
                return json(['status' => 0, 'msg' => '请输入正确的手机号']);
            }
            
            $user = Db::table('up_u_user')->where('user_phone', $phone)->find();
            if($user){
                return json(['status' => 0, 'msg' => '该手机号已注册']);
            }
        }
        
        //验证验证码
        $res = Db::table('up_phonecode')
            ->where('phone', $phone)
            ->order('time desc')
            ->limit(1)
            ->find();
        if($code != $res['code']) {
            return json(['status' => 0, 'msg' => '验证码错误']);
        }
        // 10分钟超时
        $now = time();
        if($now - $res['time'] >= 1801) {
            return json(['status' => 0, 'msg' => '验证码超时']);
        }
        if($res['status'] == 1) {
            return json(['status' => 0, 'msg' => '验证码已使用']);
        }
        
        if($type == 1){
            return json(['status' => 200, 'msg' => '通过验证']);
        }else{
            $res = Db::table('up_u_user')->where('user_id', $this->user_id)->update(['user_phone' => $phone]);
            if($res){
                $userInfo = Db::table('up_u_user')->where('user_id', $this->user_id)->find();
                return json(['status' => 200, 'msg' => $userInfo]);
            }else{
                return json(['status' => 0, 'msg' => '更换失败']);
            }
        }
    }
    
    /**
     * 退出修改状态
     * @return \think\response\Json
     */
    public function loginOut(){
        $res = Db::table('up_u_user')->where('user_id', $this->user_id)->setField('user_online', 2);
        if($res){
            return json(['status' => 200, 'msg' => '退出成功']);
        }else{
            return json(['status' => 0, 'msg' => '退出失败']);
        }
    }
    
    /**
     * 我的交易记录
     * @return \think\response\Json
     */
    public function myTransaction(){
        $tranList = Db::table('up_order')
            ->where('order_user_id', $this->user_id)
            ->field('order_type, order_price, order_time, order_up_down')
            ->order('order_time desc')
            ->page($this->page, 10)
            ->select();
        if($tranList){
            return json(['status' => 200, 'msg' => $tranList]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 个人详情展示
     * @return \think\response\Json
     */
    public function showUserInfo(){
        $info = Db::table('up_u_user')
            ->join('up_grade', 'user_grade = grade_id', 'left')
            ->join('up_subject', 'user_subject = subject_id', 'left')
            ->where('user_id', $this->user_id)
            ->field('user_image, user_username, user_gender, user_birthday, user_district_str, 
            grade_name, user_intro, subject_name, user_sheng, user_shi, user_qu, user_subject, 
            user_grade, userid')
            ->find();
        return json(['status' => 200, 'msg' => $info]);
    }
    
    /**
     * 编辑用户信息
     * @return \think\response\Json
     * user_image      头像
     * user_username   名称
     * user_gender     性别   1 男   2 女
     * user_birthday   生日
     * sheng           省
     * shi             市
     * qu              区
     * grade_id        年级
     * user_intro      简介
     * subject_id      学科
     */
    public function changeUserInfo(){
        $user_image = input('user_image');
        $user_username = input('user_username');
        $user_gender = input('user_gender');
        $user_birthday = input('user_birthday');
        $sheng = input('sheng');
        $shi = input('shi');
        $qu = input('qu');
        $grade_id = input('grade_id');
        $user_intro = trim(input('user_intro'), ' ');
        $user_subject = input('subject_id');
        $user_district_str = $this->areaStr($sheng, $shi, $qu);
        
        $data = [
            'user_image' => $user_image,
            'user_username' => $user_username,
            'user_gender' => $user_gender,
            'user_birthday' => $user_birthday,
            'user_district_str' => $user_district_str,
            'user_grade' => $grade_id,
            'user_intro' => $user_intro,
            'user_sheng' => $sheng,
            'user_shi' => $shi,
            'user_qu' => $qu,
            'user_subject' => $user_subject,
            'user_update_time' => date('Y-m-d H:i:s')
        ];
        $res = Db::table('up_u_user')->where('user_id', $this->user_id)->update($data);
        if($res){
            return json(['status' => 200, 'msg' => '保存成功']);
        }else{
            return json(['status' => 0, 'msg' => '保存失败']);
        }
    }
    
    /**
     * 学习计划展示
     * @return \think\response\Json
     */
    public function showLearnPlan(){
        $user_plan = Db::table('up_u_user')->where('user_id', $this->user_id)->value('user_plan');
        if($user_plan){
            return json(['status' => 200, 'msg' => $user_plan]);
        }else{
            return json(['status' => 200, 'msg' => '']);
        }
    }
    
    /**
     * 编辑学习计划
     * @return \think\response\Json
     * learn_plan    学习计划
     */
    public function doLearnPlan(){
        $learn_plan = trim(input('learn_plan'), ' ');
        $res = Db::table('up_u_user')->where('user_id', $this->user_id)->setField('user_plan', $learn_plan);
        if($res){
            return json(['status' => 200, 'msg' => '保存成功']);
        }else{
            return json(['status' => 0, 'msg' => '保存失败']);
        }
    }
    
    /**
     * 我购买的公开课
     * @return \think\response\Json
     * page   页码
     */
    public function myPurchase(){
        $where['purchase_user_id'] = $this->user_id;
        $where['purchase_type'] = 1;
        $where['public_type'] = 2;
        $list = Db::table('up_purchase')
            ->join('up_public_class', 'f_id = public_id', 'left')
            ->where($where)
            ->field('public_name, public_img, public_intro, public_type, public_price, 
            public_id, public_class_play_num')
            ->page($this->page, 10)
            ->order('purchase_time desc')
            ->select();
        if($list){
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 我购买的题库
     * @return \think\response\Json
     * page    页码
     */
    public function myQuestion(){
        $where['purchase_user_id'] = $this->user_id;
        $where['purchase_type'] = 2;
        $list = Db::table('up_purchase')
            ->join('up_question_bank', 'f_id = question_id', 'left')
            ->where($where)
            ->field('question_id, question_title, question_type, question_price, purchase_time')
            ->page($this->page, 10)
            ->order('purchase_time desc')
            ->select();
        if($list){
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 我购买的一对一
     * @return \think\response\Json
     * page    页码
     */
    public function myOneToOne(){
        $where['purchase_user_id'] = $this->user_id;
        $where['purchase_type'] = 3;
        $list = Db::table('up_purchase')
            ->join('up_one_to_one', 'f_id = one_id', 'left')
            ->join('up_u_user', 'one_teacher_id = user_id', 'left')
            ->join('up_subject', 'subject_id = user_subject', 'left')
            ->join('up_grade', 'user_grade = grade_id', 'left')
            ->where($where)
            ->field('user_id, user_username, user_image, user_gender, user_status, user_intro, 
            user_toll, purchase_time, subject_name, grade_name, purchase_time')
            ->page($this->page, 10)
            ->order('purchase_time desc')
            ->select();
        if($list){
            // 老师星级
            foreach($list as $k => $v){
                $list[$k]['star_num'] = $this->teacherStar($v['user_id']);
            }
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 我的求助
     * @return \think\response\Json
     * page   页码
     */
    public function myCourse(){
        $where['purchase_user_id'] = $this->user_id;
        $where['purchase_type'] = 4;
        $where['course_status'] = ['NEQ', 3]; // 已关闭的不展示
        $list = Db::table('up_purchase')
            ->join('up_course', 'f_id = course_id', 'left')
            ->join('up_u_user', 'course_teacher_id = user_id', 'left')
            ->where($where)
            ->field('course_id, course_time, course_gold, user_username, course_status, course_type')
            ->page($this->page, 10)
            ->order('purchase_time desc')
            ->select();
        if($list){
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 我下载的题库列表
     * @return \think\response\Json
     * page   页码
     */
    public function downQuestion(){
        $where['down_user_id'] = $this->user_id;
        $where['down_type'] = 1;
//        $where['down_status'] = 2;
        $list = Db::table('up_down')
            ->join('up_question_bank', 'question_id = down_object_id', 'left')
            ->field('question_id, question_title, question_time')
            ->where($where)
            ->order('down_time desc')
            ->group('question_id')
            ->page($this->page, 10)
            ->select();
        
        // 正在下载文件的个数
        $num = count($this->downingQuestion(1));
        if($list){
            return json(['status' => 200, 'msg' => $list, 'num' => $num]);
        }else{
            return json(['status' => 200, 'msg' => [], 'num' => $num]);
        }
    }
    
    /**
     * 我下载的题库文件列表
     * @return \think\response\Json
     * page   页码
     * question_id   题库ID
     */
    public function downFiles(){
        $question_id = input('question_id');
        $where['down_object_id'] = $question_id;
        $where['down_type'] = 1;
        $where['down_user_id'] = $this->user_id;
        
        // 题库信息
        $info = Db::table('up_question_bank')
            ->where('question_id', $question_id)
            ->field('question_title, question_time, question_type, question_id')
            ->find();
        if($info){
            // 文件
            $files = Db::table('up_down')
                ->join('up_question_file', 'q_file_id = down_son_id', 'left')
                ->field('q_file_name, q_file_dis, q_file_url, q_file_size, down_status')
                ->where($where)
                ->order('down_time desc')
                ->page($this->page, 10)
                ->select();
            if($files){
                $info['files'] = $files;
                return json(['status' => 200, 'msg' => $info]);
            }else{
                $info['files'] = [];
                return json(['status' => 200, 'msg' => $info]);
            }
        }else{
            return json(['status' => 0, 'msg' => '题库信息不存在']);
        }
    }
    
    /**
     * 删除我下载的题库
     * @return \think\response\Json
     * question_id   题库ID
     */
    public function delDownFile(){
        $question_id = input('question_id');
        $where['down_object_id'] = $question_id;
        $where['down_type'] = 1;
        $where['down_user_id'] = $this->user_id;
        $res = Db::table('up_down')
            ->where($where)
            ->delete();
        if($res){
            return json(['status' => 200, 'msg' => '删除成功']);
        }else{
            return json(['status' => 0, 'msg' => '删除失败']);
        }
    }
    
    /**
     * 下载中的题库文件列表
     * @return \think\response\Json
     */
    public function downingQuestionList(){
        $list = $this->downingQuestion(2);
        if($list){
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 删除下载中的记录
     * @return \think\response\Json
     * down_ids   需要删除的下载记录ID 逗号拼接
     */
    public function delDowning(){
        $down_ids = input('down_ids');
        $res = Db::table('up_down')->where('down_id', 'IN', $down_ids)->delete();
        if($res){
            return json(['status' => 200, 'msg' => '删除成功']);
        }else{
            return json(['status' => 0, 'msg' => '删除失败']);
        }
    }
    
    /**
     * 我下载的公开课列表
     * @return \think\response\Json
     * page  页码
     */
    public function downPublic(){
        $where['down_user_id'] = $this->user_id;
        $where['down_type'] = 2;
//        $where['down_status'] = 2;
        $list = Db::table('up_down')
            ->join('up_public_class', 'public_id = down_object_id', 'left')
            ->field('public_id, public_name, public_img, public_type, public_price')
            ->where($where)
            ->order('down_time desc')
            ->group('public_id')
            ->page($this->page, 10)
            ->select();
    
        // 正在下载节的个数
        $num = count($this->downingPublic(1));
        if($list){
            // 下载的节数
            foreach($list as $k => $v){
                $count = Db::table('up_down')
                    ->where('down_user_id', $this->user_id)
                    ->where('down_type', 2)
                    ->where('down_object_id', $v['public_id'])
                    ->count();
                $list[$k]['section_num'] = $count;
            }
            return json(['status' => 200, 'msg' => $list, 'num' => $num]);
        }else{
            return json(['status' => 200, 'msg' => [], 'num' => $num]);
        }
    }
    
    /**
     * 我下载的公开课节列表
     * @return \think\response\Json
     * page   页码
     * public_id   公开课ID
     */
    public function downSection(){
        $public_id = input('public_id');
        $where['down_object_id'] = $public_id;
        $where['down_type'] = 2;
        $where['down_user_id'] = $this->user_id;
        
        // 公开课信息
        $info = Db::table('up_public_class')
            ->where('public_id', $public_id)
            ->field('public_img, public_name, public_type, public_price, public_id')
            ->find();
        if($info){
            // 节
            $sections = Db::table('up_down')
                ->join('up_public_section', 'section_id = down_son_id', 'left')
                ->field('section_id, section_name, section_video, section_size, section_long_time')
                ->where($where)
                ->order('down_time desc')
                ->page($this->page, 10)
                ->select();
            if($sections){
                $info['section_num'] = count($sections);
                $info['sections'] = $sections;
                return json(['status' => 200, 'msg' => $info]);
            }else{
                $info['section_num'] = 0;
                $info['sections'] = [];
                return json(['status' => 200, 'msg' => $info]);
            }
        }else{
            return json(['status' => 0, 'msg' => '公开课信息不存在']);
        }
    }
    
    /**
     * 删除我下载的节
     * @return \think\response\Json
     * section_id  节ID
     */
    public function delDownSection(){
        $section_id = input('section_id');
        $where['down_son_id'] = $section_id;
        $where['down_type'] = 2;
        $where['down_user_id'] = $this->user_id;
        $res = Db::table('up_down')
            ->where($where)
            ->delete();
        if($res){
            return json(['status' => 200, 'msg' => '删除成功']);
        }else{
            return json(['status' => 0, 'msg' => '删除失败']);
        }
    }
    
    /**
     * 下载中的公开课节列表
     * @return \think\response\Json
     */
    public function downingPublicList(){
        $list = $this->downingPublic(2);
        if($list){
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 常见问题列表
     * @return \think\response\Json
     */
    public function problem(){
        $list = Db::table('up_problem')
            ->field('problem_id, problem_describe')
            ->order('create_time asc')
            ->where('is_end', 1)
            ->select();
        if($list){
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 常见问题详情
     * @return \think\response\Json
     * problem_id   问题ID
     */
    public function problemDetail(){
        $problem_id = input('problem_id');
        $detail = Db::table('up_problem')->where('problem_id', $problem_id)->value('problem_answer');
        if($detail){
            return json(['status' => 200, 'msg' => $detail]);
        }else{
            return json(['status' => 200, 'msg' => '']);
        }
    }
    
    /**
     * 问题反馈分类
     * @return \think\response\Json
     */
    public function feedbackType(){
        $cate = Db::table('up_feedback_type')->select();
        if($cate){
            return json(['status' => 200, 'msg' => $cate]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 举报操作
     * @return \think\response\Json
     * type     举报类型
     * content  举报内容
     * img      举报图片
     * phone    联系方式
     */
    public function doFeedback(){
        $type = input('type');
        $content = input('content');
        $img = input('img');
        $phone = input('phone');
        $insert = [
            'f_user_id' => $this->user_id,
            'feedback_phone' => $phone,
            'feedback_content' => $content,
            'feedback_time' => date('Y-m-d H:i:s'),
            'feedback_end' => 1,
            'feedback_type_id' => $type,
            'feedback_img' => $img
        ];
        $res = Db::table('up_feedback')->insert($insert);
        if($res){
            return json(['status' => 200, 'msg' => '反馈成功']);
        }else{
            return json(['status' => 0, 'msg' => '反馈失败']);
        }
    }
    
    /**
     * 功能介绍
     * @return \think\response\Json
     */
    public function studentAbout(){
        $about = Db::table('up_about')
            ->where('about_id', 1)
            ->field('about_student_gn')
            ->find();
        return json(['status' => 200, 'msg' => $about]);
    }
    
    /**
     * 法律声明
     * @return \think\response\Json
     */
    public function studentAboutF(){
        $about = Db::table('up_about')
            ->where('about_id', 2)
            ->field('about_student_law')
            ->find();
        return json(['status' => 200, 'msg' => $about]);
    }
    
    /**
     * 平台协议
     * @return \think\response\Json
     */
    public function studentAboutP(){
        $about = Db::table('up_about')
            ->where('about_id', 3)
            ->field('about_student_platform')
            ->find();
        return json(['status' => 200, 'msg' => $about]);
    }
    
    /**
     * 系统消息列表
     * @return \think\response\Json
     * page   页码
     */
    public function systemNoticeList(){
        $where['n_range'] = 1; // 学生端的系统信息
        $where['n_del_user_id'] = ['NOTLIKE', '%' . ',' . $this->user_id . ',' . '%']; // 未删除
        $where['n_type'] = 1; // 系统消息
        $where['n_user_id'] = ['LIKE', '%' . ',' . $this->user_id . ',' . '%']; // 属于我的
        $noticeList = Db::table('up_notice')
            ->where($where)
            ->field('n_id, n_title, n_send_datetime, n_read_user_id, n_content')
            ->order('n_send_datetime desc')
            ->page($this->page, 10)
            ->select();
        if($noticeList){
            // 已读、未读
            foreach($noticeList as $k => $v){
                $readed = explode(',', trim($v['n_read_user_id'], ','));
                if(in_array($this->user_id, $readed)){
                    $noticeList[$k]['is_read'] = 1; // 已读
                }else{
                    $noticeList[$k]['is_read'] = 2; // 未读
                }
            }
            return json(['status' => 200, 'msg' => $noticeList]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 系统消息详情
     * @return \think\response\Json
     * n_id    消息ID
     */
    public function systemNoticeDetail(){
        $n_id = input('n_id');
        // 消息详情
        $info = Db::table('up_notice')
            ->field('n_read_user_id, n_title, n_content, n_send_datetime')
            ->where('n_id', $n_id)
            ->find();
        if($info){
            // 修改未读状态
            $readed = explode(',', trim($info['n_read_user_id'], ','));
            if(!in_array($this->user_id, $readed)){
                $read_user = ',' . implode(',', $readed) . ',' . $this->user_id . ',';
                $res = Db::table('up_notice')->where('n_id', $n_id)->setField('n_read_user_id', $read_user);
                if($res){
                    return json(['status' => 200, 'msg' => $info]);
                }else{
                    return json(['status' => 0, 'msg' => '操作数据库失败']);
                }
            }else{
                return json(['status' => 200, 'msg' => $info]);
            }
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 清空系统消息
     * @return \think\response\Json
     */
    public function systemNoticeDel(){
        // 所有属于我的，没有被删除的消息
        $where['n_range'] = 1; // 学生端的系统信息
        $where['n_del_user_id'] = ['NOTLIKE', '%' . ',' . $this->user_id . ',' . '%']; // 未删除
        $where['n_type'] = 1; // 系统消息
        $where['n_user_id'] = ['LIKE', '%' . ',' . $this->user_id . ',' . '%']; // 属于我的
        $all = Db::table('up_notice')
            ->where($where)
            ->column('n_id, n_del_user_id');
        if($all){
            Db::startTrans();
            try{
                foreach($all as $k => $v){
                    $del = $v . $this->user_id . ',';
                    Db::table('up_notice')->where('n_id', $k)->setField('n_del_user_id', $del);
                }
                $res = 1;
                Db::commit();
            }catch(\Exception $e){
                $res = 2;
                Db::rollback();
            }
            if($res == 1){
                return json(['status' => 200, 'msg' => '清空成功']);
            }else{
                return json(['status' => 0, 'msg' => '清空失败']);
            }
        }else{
            return json(['status' => 0, 'msg' => '暂无数据']);
        }
        
    }
    
    /**
     * 通知消息列表
     * @return \think\response\Json
     * page   页码
     */
    public function informNoticeList(){
        $where['n_range'] = 1; // 学生端的系统信息
        $where['n_del_user_id'] = ['NOTLIKE', '%' . ',' . $this->user_id . ',' . '%']; // 未删除
        $where['n_type'] = 2; // 通知消息
        $where['n_user_id'] = $this->user_id; // 属于我的
        $noticeList = Db::table('up_notice')
            ->where($where)
            ->field('n_id, n_title, n_send_datetime, n_read_user_id, n_subject_type, n_subject_id, n_content')
            ->order('n_send_datetime desc')
            ->page($this->page, 10)
            ->select();
        if($noticeList){
            // 已读、未读
            foreach($noticeList as $k => $v){
                $readed = explode(',', trim($v['n_read_user_id'], ','));
                if(in_array($this->user_id, $readed)){
                    $noticeList[$k]['is_read'] = 1; // 已读
                }else{
                    $noticeList[$k]['is_read'] = 2; // 未读
                }
            }
            return json(['status' => 200, 'msg' => $noticeList]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 通知消息详情
     * @return \think\response\Json
     * n_id    消息ID
     */
    public function informNoticeDetail(){
        $n_id = input('n_id');
        // 消息详情
        $info = Db::table('up_notice')
            ->field('n_read_user_id, n_title, n_content, n_send_datetime, n_content')
            ->where('n_id', $n_id)
            ->find();
        if($info){
            // 修改未读状态
            $readed = explode(',', trim($info['n_read_user_id'], ','));
            if(!in_array($this->user_id, $readed)){
                $read_user = ',' . implode(',', $readed) . ',' . $this->user_id . ',';
                $res = Db::table('up_notice')->where('n_id', $n_id)->setField('n_read_user_id', $read_user);
                if($res){
                    return json(['status' => 200, 'msg' => $info]);
                }else{
                    return json(['status' => 0, 'msg' => '操作数据库失败']);
                }
            }else{
                return json(['status' => 200, 'msg' => $info]);
            }
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 清空通知消息
     * @return \think\response\Json
     */
    public function informNoticeDel(){
        // 所有属于我的，没有被删除的消息
        $where['n_range'] = 1; // 学生端的系统信息
        $where['n_del_user_id'] = ['NOTLIKE', '%' . ',' . $this->user_id . ',' . '%']; // 未删除
        $where['n_type'] = 2; // 通知消息
        $where['n_user_id'] = $this->user_id; // 属于我的
        $all = Db::table('up_notice')
            ->where($where)
            ->column('n_id, n_del_user_id');
        if($all){
            Db::startTrans();
            try{
                foreach($all as $k => $v){
                    $del = $v . $this->user_id . ',';
                    Db::table('up_notice')->where('n_id', $k)->setField('n_del_user_id', $del);
                }
                $res = 1;
                Db::commit();
            }catch(\Exception $e){
                $res = 2;
                Db::rollback();
            }
            if($res == 1){
                return json(['status' => 200, 'msg' => '清空成功']);
            }else{
                return json(['status' => 0, 'msg' => '清空失败']);
            }
        }else{
            return json(['status' => 0, 'msg' => '暂无数据']);
        }
    }
    
    /**
     * 返回市ID 用于定位
     * @return \think\response\Json
     */
    public function getShiId(){
        $shi = input('shi');
        $id = Db::table('up_area')->where('district', $shi)->value('district_id');
        if($id){
            return json(['status' => 200, 'msg' => $id]);
        }else{
            return json(['status' => 200, 'msg' => '定位失败']);
        }
    }
    
    /**
     * 联系我们
     * @return \think\response\Json
     */
    public function contactUs(){
        $phone = Db::table('up_about')->where('about_id', 7)->value('about_phone');
        if($phone){
            return json(['status' => 200, 'msg' => $phone]);
        }else{
            return json(['status' => 200, 'msg' => '']);
        }
    }
    
    /**
     * 用于首页展示小红点
     * @return \think\response\Json
     */
    public function redPoint(){
        $where['n_range'] = 1;
        $where['n_read_user_id'] = ['notlike', '%,' . $this->user_id . ',%'];
        $where['n_del_user_id'] = ['notlike', '%,' . $this->user_id . ',%'];
        $noticeList = Db::table('up_notice')
            ->where($where)
            ->where(function ($query){
                $query->where('n_user_id', $this->user_id)
                ->whereOr('n_user_id', 'like', '%,' . $this->user_id . ',%');
            })
            ->select();
        if($noticeList){ // 显示红点
            $red = 1;
        }else{ // 不显示红点
            $red = 2;
        }
        return json(['status' => 200, 'msg' => $red]);
    }
    
    /**
     * 展示小红点---系统消息
     * @return \think\response\Json
     */
    public function redPointSys(){
        $where['n_range'] = 1;
        $where['n_type'] = 1;
        $where['n_read_user_id'] = ['notlike', '%,' . $this->user_id . ',%'];
        $where['n_del_user_id'] = ['notlike', '%,' . $this->user_id . ',%'];
        $where['n_user_id'] = ['like', '%,' . $this->user_id . ',%'];
        $noticeList = Db::table('up_notice')
            ->where($where)
            ->select();
        if($noticeList){ // 显示红点
            $red = 1;
        }else{ // 不显示红点
            $red = 2;
        }
        return json(['status' => 200, 'msg' => $red]);
    }
    
    /**
     * 展示小红点---通知消息
     * @return \think\response\Json
     */
    public function redPointInform(){
        $where['n_range'] = 1;
        $where['n_type'] = 2;
        $where['n_read_user_id'] = ['notlike', '%,' . $this->user_id . ',%'];
        $where['n_del_user_id'] = ['notlike', '%,' . $this->user_id . ',%'];
        $where['n_user_id'] = $this->user_id;
        $noticeList = Db::table('up_notice')
            ->where($where)
            ->select();
        if($noticeList){ // 显示红点
            $red = 1;
        }else{ // 不显示红点
            $red = 2;
        }
        return json(['status' => 200, 'msg' => $red]);
    }
    
    /**
     * 根据老师ID获取老师信息
     * @return \think\response\Json
     */
    public function getOtherInfo(){
        $other_user_id = input('other_user_id');
        $info = Db::table('up_u_user')
            ->where('user_id', $other_user_id)
            ->field('user_username, user_image')
            ->find();
        return json(['status' => 200, 'msg' => $info]);
    }
    
    /**
     * 学生隐私权政策
     * @return \think\response\Json
     */
    public function yinsi(){
        $yinsi = Db::table('up_about')->where('about_id', 11)->value('about_student_yinsi');
        return json(['status' => 200, 'msg' => $yinsi]);
    }
    
    /**
     * 学生服务协议
     * @return \think\response\Json
     */
    public function serve(){
        $serve = Db::table('up_about')->where('about_id', 13)->value('about_student_serve');
        return json(['status' => 200, 'msg' => $serve]);
    }
    
    /**
     * 视频播放次数
     * @return \think\response\Json
     * section_id    视频ID
     * public_id     课程ID
     */
    public function playNum(){
        $section_id = input('section_id');
        $public_id = input('public_id');
        // 课程
        Db::table('up_public_class')->where('public_id', $public_id)->setInc('public_class_play_num');
        // 节
        Db::table('up_public_section')->where('section_id', $section_id)->setInc('section_play_num');
        return json(['status' => 200, 'msg' => true]);
    }
    
    /**
     * 我的正在下载的题库文件 ----用于列表和数量
     * @return false|\PDOStatement|string|\think\Collection
     * type   1 数量   2 列表
     */
    private function downingQuestion($type){
        $where['down_type'] = 1;
        $where['down_user_id'] = $this->user_id;
        $where['down_status'] = 1;
        if($type == 1){
            $downing = Db::table('up_down')->where($where)->select();
        }else{
            $downing = Db::table('up_down')
                ->join('up_question_file', 'q_file_id = down_son_id', 'left')
                ->field('down_id, q_file_id, q_file_name, q_file_url, q_file_dis, q_file_size')
                ->where($where)
                ->page($this->page, 10)
                ->order('down_time desc')
                ->select();
        }
        return $downing;
    }
    
    /**
     * 我的正在下载的公开课节 ----用于列表和数量
     * @return false|\PDOStatement|string|\think\Collection
     * type   1 数量   2 列表
     */
    private function downingPublic($type){
        $where['down_type'] = 2;
        $where['down_user_id'] = $this->user_id;
        $where['down_status'] = 1;
        if($type == 1){
            $downing = Db::table('up_down')->where($where)->select();
        }else{
            $downing = Db::table('up_down')
                ->join('up_public_section', 'section_id = down_son_id', 'left')
                ->field('down_id, section_id, section_name, section_size, section_video, section_long_time')
                ->where($where)
                ->page($this->page, 10)
                ->order('down_time desc')
                ->select();
        }
        return $downing;
    }
    
    /**
     * 省市区字符串
     * @param $sheng
     * @param $shi
     * @param $qu
     * @return string
     */
    private function areaStr($sheng, $shi, $qu)
    {
        $shengName = Db::name('area')->where('district_id', $sheng)->value('district');
        $shiName = Db::name('area')->where('district_id', $shi)->value('district');
        $quName = Db::name('area')->where('district_id', $qu)->value('district');
        $str = $shengName . ' ' . $shiName . ' ' . $quName;
        return $str;
    }
}